package com.cwx.mychat.controller;

import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.FileStatusEnum;
import com.cwx.mychat.entity.po.FileCloud;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.FileCloudService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 作者： cwx
 * 时间： 2025/5/21 16:56
 * 版本： 1.0
 * 注释：
 */
@RestController
@RequestMapping("/filecloud")
public class FileCloudController {


    @Autowired
    private FileCloudService fileCloudService;


    /**
     * 新建文件夹
     */
    @PostMapping("/folder")
    public R createFolder(@RequestBody FileCloud fileCloud){
        fileCloud.setFileStatus(FileStatusEnum.NORMAL.getStatus());
        fileCloud.setCreateTime(new Date());
        fileCloud.setUpdateTime(new Date());
        fileCloudService.save(fileCloud);
        return R.ok();
    }

    /**
     * 查询文件
     */
    @GetMapping("/list")
    public R list(@RequestParam String userId,@RequestParam String parentId){
        return fileCloudService.list(userId,parentId);
    }


    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public R upload(@NotNull MultipartFile file,
                    @RequestParam String userId,
                    @RequestParam String parentId,
                    @RequestParam String userName,
                    @RequestParam String fileType,
                    @RequestParam String state) throws Exception {
        return fileCloudService.upload(file,userId,parentId,userName,fileType, state);

    }
    /**
     * 删除文件
     */
    @DeleteMapping("/deleteFile/{fileId}")
    public R delete(@PathVariable String fileId, @RequestParam String userId) throws BusinessException {
        //判断是否是本机人删除文件
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        if(!tokenUserInfoDto.getUserId().equals(userId)){
            return R.error("无权限删除");
        }
        return fileCloudService.delete(fileId, userId);
    }

    /**
     * 获取文件详细信息
     * @param fileId
     * @return
     */
    @GetMapping("/detail/{fileId}")
    public R getInfoDetail(@PathVariable String fileId){
        FileCloud byId = fileCloudService.getById(fileId);
        byId.setFilePath(null);
        return R.ok().put(CodeConstant.RESULT_MSG, byId);
    }

    /**
     * 删除文件夹
     * @param folderId
     * @param userId
     * @return
     * @throws BusinessException
     */
    @DeleteMapping("/deleteFolder/{folderId}")
    public R deleteFolder(@PathVariable String folderId,@RequestParam String userId) throws BusinessException {
        return fileCloudService.deleteFolder(folderId,userId);
    }


    /**
     * 下载文件
     * @param fileId
     * @param response
     * @return
     */
    @GetMapping("/download/{fileId}")
    public ResponseEntity<InputStreamResource> download(@PathVariable String fileId,
                                                        @RequestParam String userId,
                                                        @NotNull HttpServletResponse response) throws Exception {
        return fileCloudService.download(fileId, userId, response);
    }

}
